﻿#pragma once
#include <iostream>
#include <vector>
using namespace std;

namespace hash_bucket
{
	template<class T>
	struct HashNode
	{
		T _data;
		HashNode<T>* _next;

		HashNode(const T& data)
			:_data(data)
			, _next(nullptr)
		{
		}
	};

	template<class K>
	class DefaultHash
	{
	public:
		size_t operator()(const K& key)
		{
			return (size_t)key;
		}
	};

	template<>
	class DefaultHash<string>
	{
	public:
		size_t operator()(const string& str)
		{
			size_t key = 0;
			for (int i = 0; i < str.size(); i++)
			{
				key *= 131;
				key += str[i];
			}
			return key;
		}
	};

	// 为了实现简单，在哈希桶的迭代器类中需要用到hashBucket本身，
	template<class K, class T, class KeyOfT, class Hash>
	class HashBucket;

	// 注意：因为哈希桶在底层是单链表结构，所以哈希桶的迭代器不需要--操作
	template <class K, class T,class Ref,class Ptr,class KeyOfT, class Hash>
	struct HBIterator
	{
		typedef HashBucket<K, T, KeyOfT, Hash> HashBucket;
		typedef HashNode<T> Node;
		typedef HBIterator<K, T,Ref,Ptr, KeyOfT, Hash> Self;
		typedef HBIterator<K, T, T&, T*, KeyOfT, Hash> iterator;


		Node* _node;             // 当前迭代器关联的节点
		const HashBucket* _pHt;         // 哈希桶--主要是为了找下一个空桶时候方便

		HBIterator(Node* node, const HashBucket* pHt)
			:_node(node)
			,_pHt(pHt)
		{
			;
		}

		HBIterator(const iterator& it)
			:_node(it._node)
			, _pHt(it._pHt)
		{
			;
		}

		Self& operator++()
		{
			if (_node->_next)
			{
				_node = _node->_next;
				return *this;
			}
			else
			{
				KeyOfT kot;
				Hash hs;
				size_t hashi = hs(kot(_node->_data)) % (_pHt->_tables.size());
				++hashi;

				while (hashi < _pHt->_tables.size())
				{
					if (_pHt->_tables[hashi] != nullptr)
					{
						_node= _pHt->_tables[hashi];
						return *this;
					}
					else
					{
						++hashi;
					}
				}

				_node = nullptr;
				return *this;
			}
		}

		//Self operator++(int);

		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}

		bool operator==(const Self& it) const
		{
			return _node == it._node;
		}

		bool operator!=(const Self& it) const
		{
			return _node != it._node;
		}
	};

	// K 为 T 中key的类型
	// T 可能是键值对，也可能是K
	// KeyOfT: 从T中提取key
	// Hash将key转化为整形，因为哈希函数使用除留余数法
	template<class K, class T, class KeyOfT, class Hash=DefaultHash<K>>
	class HashBucket
	{
		template <class K, class T, class Ref, class Ptr, class KeyOfT, class Hash>
		friend struct HBIterator;

		typedef HashNode<T> Node;
	public:
		typedef HBIterator<K, T, T&, T*, KeyOfT, Hash> iterator;
		typedef HBIterator<K, T,const T&,const T*, KeyOfT, Hash> const_iterator;


	public:
		HashBucket()
		{
			_tables.resize(10, nullptr);
		}

		// 哈希桶的销毁
		~HashBucket()
		{
			for (int i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				while (cur)
				{
					Node* next = cur->_next;
					delete cur;
					cur = next;
				}
				_tables[i] = nullptr;
			}
			_n = 0;
		}

		iterator begin()
		{
			for (int i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];

				if (cur != nullptr)
					return iterator(cur, this);
			}
			return iterator(nullptr, this);
		}

		iterator end()
		{
			return iterator(nullptr, this);
		}

		const_iterator begin()const
		{
			for (int i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];

				if (cur != nullptr)
					return const_iterator(cur, this);
			}
			return const_iterator(nullptr, this);
		}

		const_iterator end()const
		{
			return const_iterator(nullptr, this);
		}

		// 插入值为data的元素，如果data存在则不插入
		pair<iterator,bool> Insert(const T& data)
		{
			KeyOfT kot;
			Hash hs;

			iterator it = Find(kot(data));
			if (it != end())
			{
				return make_pair(it, false);
			}


			//扩容
			if (_n == _tables.size())
			{
				size_t newsize = _tables.size() * 2;

				vector<Node*> newtables;
				newtables.resize(newsize, nullptr);

				for (int i = 0; i < _tables.size(); i++)
				{
					Node* cur = _tables[i];
					while (cur)
					{
						Node* next = cur->_next;
						size_t hashi = hs(kot(cur->_data)) % newtables.size();
						cur->_next = newtables[hashi];
						newtables[hashi] = cur;
						cur = next;
					}
				}

				_tables.swap(newtables);
			}

			//插入
			size_t hashi = hs(kot(data)) % _tables.size();
			Node* newnode = new Node(data);
			newnode->_next = _tables[hashi];
			_tables[hashi] = newnode;
			++_n;

			return make_pair(iterator(newnode,this),true);
		}

		// 在哈希桶中查找值为key的元素，存在返回true否则返回false﻿
		iterator Find(const K& key)
		{
			KeyOfT kot;
			Hash hs;

			size_t hashi = hs(key) % _tables.size();
			Node* cur = _tables[hashi];

			while (cur)
			{
				if ((kot(cur->_data)) == key)
				{
					return iterator(cur,this);
				}
				cur = cur->_next;
			}

			return end();
		}

		// 哈希桶中删除key的元素，删除成功返回true，否则返回false
		bool Erase(const K& key)
		{
			Hash hs;
			KeyOfT kot;

			size_t hashi = hs(key) % _tables.size();

			Node* cur = _tables[hashi];
			Node* prev = cur;
			while (cur)
			{
				if ((kot(cur->_data)) == key)
				{
					if (prev == cur)
					{
						_tables[hashi] = cur->_next;
						delete cur;
						--_n;
						return true;
					}

					prev->_next = cur->_next;
					delete cur;
					--_n;
					return true;
				}

				prev = cur;
				cur = cur->_next;
			}

			return false;
		}

	private:
		vector<Node*> _tables;  // 指针数组
		size_t _n = 0;			// 表中存储数据个数
	};
}

